package com.ds.topmenu.database;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.admin.temp.JavaTempNavTree;
import com.ds.dsm.manager.repository.RepositoryGridView;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTreeViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.enums.DSMType;
import com.ds.esd.dsm.repository.RepositoryInst;
import com.ds.web.annotation.Aggregation;
import com.ds.web.annotation.AggregationType;
import com.ds.web.annotation.Split;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping(value = {"/action/database/codegen/"})
@Aggregation(type=AggregationType.menu)
public class CodeGenAction {


    @RequestMapping(method = RequestMethod.POST, value = "DBInstList")
    @GridViewAnnotation
    @CustomAnnotation(index = 0)
    @ModuleAnnotation(imageClass = "iconfont iconchucun", caption = "代码模板")
    @ResponseBody
    public ListResultModel<List<RepositoryGridView>> getDBInstList(String projectName) {
        ListResultModel<List<RepositoryGridView>> result = new ListResultModel();
        try {
            List<RepositoryInst> tempBeans = DSMFactory.getInstance().getRepositoryManager().getRepositoryInstList(projectName);
            result = PageUtil.getDefaultPageList(tempBeans, RepositoryGridView.class);
        } catch (JDSException e) {
            e.printStackTrace();
            result = new ErrorListResultModel<>();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = {"split"})
    @Split
    @CustomAnnotation(index = 1)
    @ResponseBody
    public ResultModel<Boolean> split() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }


    @RequestMapping(method = RequestMethod.POST, value = "CodeTemps")
    @APIEventAnnotation(autoRun = true)
    @NavTreeViewAnnotation
    @CustomAnnotation(index = 2)
    @ModuleAnnotation(imageClass = "spafont spa-icon-settingprj", dynLoad = true, caption = "模板管理")
    @ResponseBody
    public TreeListResultModel<List<JavaTempNavTree>> getTempManager(String id) {
        TreeListResultModel<List<JavaTempNavTree>> resultModel = new TreeListResultModel<List<JavaTempNavTree>>();
        JavaTempNavTree viewItem = null;
        try {
            viewItem = new JavaTempNavTree(DSMType.repository);
            List<JavaTempNavTree> items = new ArrayList<>();
            items.add(viewItem);
            if (id != null && !id.equals("")) {
                String[] orgIdArr = StringUtility.split(id, ";");
                resultModel.setIds(Arrays.asList(orgIdArr));
            } else {
                resultModel.setIds(Arrays.asList(new String[]{viewItem.getFristClassItem(viewItem).getId()}));
            }
            resultModel.setData(items);
        } catch (JDSException e) {
            resultModel = new ErrorListResultModel<>();
            ((ErrorListResultModel<List<JavaTempNavTree>>) resultModel).setErrdes(e.getMessage());
        }

        return resultModel;

    }


}
